package com.iris.live.services.report.sources;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.GetConfigureInfoUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.DataCheckRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/24.
 */
@Component("ExportDataCheckFinanceContrastReportSource")
public class ExportDataCheckFinanceContrastReportSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDataCheckFinanceContrastReportSource.class);

    private String manualData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_金融_手工台账数据";
    private String systemData = GetConfigureInfoUtils.getConfigureInfo("file_group") + "_经销商_数据核验_金融_LiVE系统数据";

    @Autowired
    private DataCheckRepository dataCheckRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[2];
        sheets[0] = new Sheet(manualData,manualData,true);
        sheets[1] = new Sheet(systemData,systemData,true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> values = new ArrayList<>();

        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String startDate = context.get("startDate") == null ? null : context.get("startDate").toString();
        String endDate = context.get("endDate") == null ? null : context.get("endDate").toString();
        String nowDate = null;
        if (StringUtils.isNullOrBlank(startDate) && StringUtils.isNullOrBlank(endDate)) {
            nowDate = DateFormatUtils.getFormatDateToString(new java.util.Date(), "yyyyMMdd");
        }
        List list = dataCheckRepository.getFinanceContrast(permit, nowDate, startDate, endDate);

        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());

        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map<String, Object> map = (Map) o;
                if (manualData.equals(sheet.getName())) {
                    values.add(new Row(getCells(map)));
                } else if (systemData.equals(sheet.getName())) {
                    values.add(new Row(getOtherCells(map)));
                }
            }
        }
        return values;
    }

    private Cell[] getCells (Map<String, Object> map) {
        Cell[] cells = new Cell[14];
        int index = 0;
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("importLoanCustName") == null ? "" : map.get("importLoanCustName").toString(), index, !"1".equals(map.get("deductionLoanCustName") == null ? null : map.get("deductionLoanCustName").toString()));
        cells[index++] = new Cell(map.get("importLoanApprovalDate") == null ? "" : map.get("importLoanApprovalDate").toString(), index, !"1".equals(map.get("deductionLoanApprovalDate") == null ? null : map.get("deductionLoanApprovalDate").toString()));
        cells[index++] = new Cell(map.get("importFinanceCompany") == null ? "" : map.get("importFinanceCompany").toString(), index, !"1".equals(map.get("deductionFinanceCompany") == null ? null : map.get("deductionFinanceCompany").toString()));
        cells[index++] = new Cell(map.get("importDepositPayment") == null ? "" : map.get("importDepositPayment").toString(), index, !"1".equals(map.get("deductionDepositPayment") == null ? null : map.get("deductionDepositPayment").toString()));
        cells[index++] = new Cell(map.get("importDownPayment") == null ? "" : map.get("importDownPayment").toString(), index, !"1".equals(map.get("deductionDownPayment") == null ? null : map.get("deductionDownPayment").toString()));
        cells[index++] = new Cell(map.get("importLoanPayment") == null ? "" : map.get("importLoanPayment").toString(), index, !"1".equals(map.get("deductionLoanPayment") == null ? null : map.get("deductionLoanPayment").toString()));
        cells[index++] = new Cell(map.get("importLoanTerm") == null ? "" : map.get("importLoanTerm").toString(), index, !"1".equals(map.get("deductionLoanTerm") == null ? null : map.get("deductionLoanTerm").toString()));
        cells[index++] = new Cell(map.get("importYearlyRate") == null ? "" : map.get("importYearlyRate").toString(), index, !"1".equals(map.get("deductionYearlyRate") == null ? null : map.get("deductionYearlyRate").toString()));
        cells[index++] = new Cell(map.get("importBmbsDiscount") == null ? "" : map.get("importBmbsDiscount").toString(), index, !"1".equals(map.get("deductionBmbsDiscount") == null ? null : map.get("deductionBmbsDiscount").toString()));
        cells[index++] = new Cell(map.get("importDealerDiscount") == null ? "" : map.get("importDealerDiscount").toString(), index, !"1".equals(map.get("deductionDealerDiscount") == null ? null : map.get("deductionDealerDiscount").toString()));
        cells[index++] = new Cell(map.get("importAccountDate") == null ? "" : map.get("importAccountDate").toString(), index, !"1".equals(map.get("deductionAccountDate") == null ? null : map.get("deductionAccountDate").toString()));
        cells[index++] = new Cell(map.get("importFinanceFee") == null ? "" : map.get("importFinanceFee").toString(), index, !"1".equals(map.get("deductionFinanceFee") == null ? null : map.get("deductionFinanceFee").toString()));
        cells[index++] = new Cell(map.get("importFinanceServiceFee") == null ? "" : map.get("importFinanceServiceFee").toString(), index, !"1".equals(map.get("deductionFinanceServiceFee") == null ? null : map.get("deductionFinanceServiceFee").toString()));
        return cells;
    }


    private Cell[] getOtherCells (Map<String, Object> map) {
        Cell[] cells = new Cell[14];
        int index = 0;
        cells[index++] = new Cell(map.get("vin") == null ? "" : map.get("vin").toString(), index, !"02".equals(map.get("compareResult") == null ? null : map.get("compareResult").toString()));
        cells[index++] = new Cell(map.get("dbLoanCustName") == null ? "" : map.get("dbLoanCustName").toString(), index, !"1".equals(map.get("deductionLoanCustName") == null ? null : map.get("deductionLoanCustName").toString()));
        cells[index++] = new Cell(map.get("dbLoanApprovalDate") == null ? "" : map.get("dbLoanApprovalDate").toString(), index, !"1".equals(map.get("deductionLoanApprovalDate") == null ? null : map.get("deductionLoanApprovalDate").toString()));
        cells[index++] = new Cell(map.get("dbFinanceCompany") == null ? "" : map.get("dbFinanceCompany").toString(), index, !"1".equals(map.get("deductionFinanceCompany") == null ? null : map.get("deductionFinanceCompany").toString()));
        cells[index++] = new Cell(map.get("dbDepositPayment") == null ? "" : map.get("dbDepositPayment").toString(), index, !"1".equals(map.get("deductionDepositPayment") == null ? null : map.get("deductionDepositPayment").toString()));
        cells[index++] = new Cell(map.get("dbDownPayment") == null ? "" : map.get("dbDownPayment").toString(), index, !"1".equals(map.get("deductionDownPayment") == null ? null : map.get("deductionDownPayment").toString()));
        cells[index++] = new Cell(map.get("dbLoanPayment") == null ? "" : map.get("dbLoanPayment").toString(), index, !"1".equals(map.get("deductionLoanPayment") == null ? null : map.get("deductionLoanPayment").toString()));
        cells[index++] = new Cell(map.get("dbLoanTerm") == null ? "" : map.get("dbLoanTerm").toString(), index, !"1".equals(map.get("deductionLoanTerm") == null ? null : map.get("deductionLoanTerm").toString()));
        cells[index++] = new Cell(map.get("dbYearlyRate") == null ? "" : map.get("dbYearlyRate").toString(), index, !"1".equals(map.get("deductionYearlyRate") == null ? null : map.get("deductionYearlyRate").toString()));
        cells[index++] = new Cell(map.get("dbBmbsDiscount") == null ? "" : map.get("dbBmbsDiscount").toString(), index, !"1".equals(map.get("deductionBmbsDiscount") == null ? null : map.get("deductionBmbsDiscount").toString()));
        cells[index++] = new Cell(map.get("dbDealerDiscount") == null ? "" : map.get("dbDealerDiscount").toString(), index, !"1".equals(map.get("deductionDealerDiscount") == null ? null : map.get("deductionDealerDiscount").toString()));
        cells[index++] = new Cell(map.get("dbAccountDate") == null ? "" : map.get("dbAccountDate").toString(), index, !"1".equals(map.get("deductionAccountDate") == null ? null : map.get("deductionAccountDate").toString()));
        cells[index++] = new Cell(map.get("dbFinanceFee") == null ? "" : map.get("dbFinanceFee").toString(), index, !"1".equals(map.get("deductionFinanceFee") == null ? null : map.get("deductionFinanceFee").toString()));
        cells[index++] = new Cell(map.get("dbFinanceServiceFee") == null ? "" : map.get("dbFinanceServiceFee").toString(), index, !"1".equals(map.get("deductionFinanceServiceFee") == null ? null : map.get("deductionFinanceServiceFee").toString()));
        return cells;
    }
}
